﻿using FPSO.HMI.Pipe;
using FPSO.HMI.Views.Dialog;
using FPSO.Models.Domain;
using FPSO.Models.ViewModel.Controls;
using FPSO.Services;
using FPSO.Services.Impl;
using FPSO.ViewModel.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace FPSO.HMI.Controls
{
    /// <summary>
    /// UCOilBallastMin.xaml 的交互逻辑
    /// </summary>
    public partial class UCOilBallastMin : UserControl
    {
        private PipeGraph ballastGraph = null;
        private PipeGraph oilGraph = null;
        private DispatcherTimer taskTimer = null;
        private UCOilBallastViewModel viewModel = null;
        private ImitateExecuteTaskService<UCOilBallastViewModel> imitateService = null;

        public UCOilBallastMin()
        {
            InitializeComponent();

            viewModel = new UCOilBallastViewModel();
            DataContext = viewModel;

            taskTimer = new DispatcherTimer();
            taskTimer.Interval = TimeSpan.FromSeconds(1);
            taskTimer.Tick += TaskTimer_Tick;
            taskTimer.Start();
        }

        public void initTankVolumeForImitate(Dictionary<string, double> tankvols)
		{        
			foreach(KeyValuePair<string, double> tankvol in  tankvols) 
			{
				string tankname = tankvol.Key;
				double vol = tankvol.Value;
				if(viewModel.GetCotViewModel(tankname) != null)
				{
					TankOilViewModel cotTank = viewModel.GetCotViewModel(tankname);
					cotTank.Volume = vol;
				}
				else if(viewModel.GetBwtViewModel(tankname) != null)
				{
					TankViewModel bwtTank = viewModel.GetBwtViewModel(tankname);
					bwtTank.Volume = vol;
				}
			}
		}

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            if (ballastGraph == null)
            {
                ballastGraph = new PipeGraph(canvasBallast);
            }
            if (oilGraph == null)
            {
                oilGraph = new PipeGraph(canvasOil);
            }
        }

        /// <summary>
        /// 刷新一次ViewModel
        /// </summary>
        public void ReflushViewModel(ManualCalcResult calcResult)
        {
            //暂时不启用PDB
            IOilBallastService realService = new RealOilBallastServiceImpl(SysConfig.PDBConnStr, viewModel);
            realService.ReflushData(calcResult);
        }

        private void TaskTimer_Tick(object sender, EventArgs e)
        {
            if (ballastGraph != null)
            {
                ballastGraph.CalcPumpLines();
            }
            if (oilGraph != null)
            {
                oilGraph.CalcPumpLines();
            }
        }

        public void StartImitate()
        {
            DBTankTaskService dBTankTaskService = new DBTankTaskService();
            imitateService = new ImitateExecuteTaskService<UCOilBallastViewModel>(viewModel, dBTankTaskService);
        }

        public void SetImitateTime(DateTime lastRefreshTime, DateTime currentTime)
        {
            if (imitateService != null)
            {
                imitateService.ReflushData(lastRefreshTime, currentTime);
            }
        }

        public Dictionary<string, double> GetTankVolumeDic()
        {
            Dictionary<string, double> volumeDic = new Dictionary<string, double>();
            PropertyInfo[] propertys = typeof(UCOilBallastViewModel).GetProperties();
            foreach(PropertyInfo p in propertys)
            {
                object propVal = p.GetValue(viewModel);
                if (propVal is TankViewModel t)
                {
                    volumeDic.Add(t.TankId, t.Volume);
                }
                else if (propVal is TankOilViewModel o)
                {
                    volumeDic.Add(o.TankId, o.Volume);
                }
            }
            return volumeDic;
        }
		public Dictionary<string, double> GetTankRateDic()
		{
			Dictionary<string, double> rateDic = new Dictionary<string, double>();
			PropertyInfo[] propertys = typeof(UCOilBallastViewModel).GetProperties();
			foreach (PropertyInfo p in propertys)
			{
				object propVal = p.GetValue(viewModel);
				if (propVal is TankViewModel t)
				{
					rateDic.Add(t.TankId, t.Rate);
				}
				else if (propVal is TankOilViewModel o)
				{
					rateDic.Add(o.TankId, o.Rate);
				}
			}
			return rateDic;
		}
	}
}
